package com.hxs.learn.plugins.plugins.gen;

import org.beetl.core.Template;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.db.MetadataManager;
import org.beetl.sql.core.db.TableDesc;
import org.beetl.sql.core.kit.StringKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

public class MdCodeGen {
    private static Logger logger = LoggerFactory.getLogger(MdCodeGen.class);
    String CR = System.getProperty("line.separator");
    public static String serviceTemplate = "";

    static {
        serviceTemplate = GenConfig.getTemplate("/sql/md.btl");
    }

    public void genCode(SQLManager sqlManager, String pkg, String table, FileWriter writer,List<Map> attrs) throws IOException {
        Template template = SourceGen.gt.getTemplate(serviceTemplate);
        MetadataManager mm = sqlManager.getMetaDataManager();
        final TableDesc tableDesc = mm.getTable(table);
        String tableName = tableDesc.getName();
        String className = sqlManager.getNc().getClassName(tableName);

        String entityClass = sqlManager.getNc().getClassName(tableDesc.getName());
        String mapperClass = entityClass + "Mapper";
        //String condition = sqlManager.getDbStyle().genCondition(table);
        //String updateSample = sqlManager.getDbStyle().genColAssignPropertyAbsolute(table);
        //String cols = sqlManager.getDbStyle().genColumnList(table);
        template.binding("package", pkg);
        template.binding("mapperClass", mapperClass);
        template.binding("entityClassName", StringKit.toLowerCaseFirstOne(className));
        /*template.binding("table", table);
        template.binding("condition", condition);
        template.binding("updateSample", updateSample);*/
        //template.binding("cols", cols);
        template.binding("attrs", attrs);
        String mapperCode = template.render();
        writer.write(mapperCode);
        writer.flush();
    }
}
